/**
 * Created by sunkejun on 15/10/17.
 */
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
})

var ComdataUtil  = (function ($) {
    this.collectData = function(container){
        var data = {};
        $("#"+container+" input,textarea,select,img").each(function () {

            if($(this).attr('name')){
                if($(this)[0].tagName == "IMG"){
                    data[$(this).attr('name')]= $(this).attr('src');
                }
                else{
                    if(this.type == 'checkbox'){
                        data[$(this).attr('name')]= $(this).is(":checked")? 1:0;
                    }
                    else{
                        data[$(this).attr('name')]= $(this).val();
                    }
                }

            }
        });

        return data;
    }

    this.clear = function(container){
        $("#"+container+" input,textarea,select").each(function () {
            if($(this).attr('name')){
               $(this).val("");
            }
        });
    }
    this.loadContainer = function(container,obj){
        var key,value,tagName,type,arr;
        for(x in obj){
            key = x;
            value = obj[x];

           // $("[name='"+key+"'],[name='"+key+"[]']").each(function(){
            $("#"+container+" [name='"+key+"'],[name='"+key+"[]']").each(function () {
                tagName = $(this)[0].tagName;
                type = $(this).attr('type');
                if(tagName=='INPUT'){
                    if(type=='radio'){
                        $(this).attr('checked',$(this).val()==value);
                    }else if(type=='checkbox'){
                        if(value){
                            arr = value.split(',');
                            for(var i =0;i<arr.length;i++){
                                if($(this).val()==arr[i]){
                                    $(this).attr('checked',true);
                                    break;
                                }
                            }
                        }

                    }else{
                        $(this).val(value);
                    }
                }else if(tagName=='SELECT' || tagName=='TEXTAREA'){
                    $(this).val(value);
                }

            });
        }
    }

    this.ajaxpost = function(url,data,callback){

        $.ajax({
            url:  url,
            type:"POST",
            dataType:'JSON',
            data: data,
            success:function(response){
                if(response.code !=500){
                    openToastr.open(1,response.res_data);
                }
                if(callback){
                    callback(response);
                }

            }
        });
    }
    this.ajaxpostnormal = function(url,data,callback,type){

        $.ajax({
            url:  url,
            type:type?type:"POST",
            dataType:'JSON',
            data: data,
            success:function(response){
                if(callback){
                    callback(response);
                }

            }
        });
    }

    return this;
})(jQuery);
